/**
 * create by young (young2f@outlook.com) on 2020/9/12
 */


'use strict';
import React from 'react';
import {connect} from 'dva/index';
import {
  Button,
  Drawer,
  Form,
  Tag,
  Popconfirm,
  DatePicker,
  Dropdown,
  Menu,
  Input,
  Table,
  Select,
  Row,
  Col,
  Card, Statistic
} from 'antd';
import styles from '../index.less';

import ViewTitle from '../../components/ViewTitle';
import FormBase from '../../components/Base/FormBase';
import notify from "../../components/NotificationExtra";
import {FormField, ModalView, SearchForm} from 'antd-extra';
import moment from 'moment';
import 'moment/locale/zh-cn';
import locale from "antd/es/date-picker/locale/zh_CN";
import ModalSalesOrderDetail from "./modal/ModalSalesOrderDetail";

import ModalSalesOrderHistroy from "./modal/ModalSalesOrderHistroy";

moment.locale('zh-cn');
const {RangePicker} = DatePicker;
const _ = require('lodash');
const {Option} = Select;
const table = 'SalesOrder';
const title = '订单';

@Form.create()
@connect(({user, global, sales, store}) => ({...user, ...global, ...sales, ...store}))
export default class Review extends FormBase {
  constructor(props) {
    super(props);
    this.state = {};
  }

  componentDidMount() {
    const {dispatch} = this.props;
    const payload = this.props.form.getFieldsValue();

    dispatch && dispatch({
      type: 'global/getModel',
      payload: {table, tenantCode: this.getTenantCode()}
    });

    this.props.dispatch({
      type: 'sales/findMany',
      payload
    });
    dispatch && dispatch({
      type: 'dict/findMany',
      payload: {type: 'division', tenantCode: this.getTenantCode()}
    });
    this.props.dispatch({
      type: 'customer/findMany',
      payload: {}
    });
    this.props.dispatch({
      type: 'material/findMany',
      payload: {}
    });
  }


  onHistory = values => {
    const {code} = values;
    ModalView.open(ModalSalesOrderHistroy, {
      title,
      width: '920px'
    }, {code});
  };

  getTableCols = () => {
    const model = this.props[`${table}Model`];

    const temp = this.getTableColumns(model);

    temp.push(
      {
        title: '创建者',
        dataIndex: 'createUsername',
        key: 'createUsername',
        width: 120
      },
      {
        title: '创建时间',
        dataIndex: 'createdTs',
        key: 'createdTs',
        width: 160,
        render: value => {
          return <div>{moment(value).format('YYYY-MM-DD hh:mm:ss')}</div>;
        }
      },
      {
        title: '操作', dataIndex: 'operation', key: 'operation',
        width: 120, fixed: 'right',
        render: (value, record) => {
          return (
            <span>
           {<a style={{marginRight: '6px'}}
               onClick={() => this.setState({showDetail: true, record})}>详情</a>}
              {<a style={{marginRight: '6px'}}
                  onClick={() => this.onHistory(record)}>历程</a>}
            </span>
          );
        }
      });
    return temp;

  };
  getSearchFields = () => {
    const model = this.props[`${table}Model`];
    if (!model) return [];
    const arr = [];
    this.getSearchField(arr, model, "");
    const {prods} = model;
    this.getSearchField(arr, prods, "prods.");
    const {materials} = prods;
    this.getSearchField(arr, materials, "prods.materials.");
    arr.push(<FormField
      label={"是否开票"}
      name={"isKP"}
      key={"isKP"}
    >
      <Select allowClear>{[<Option value={"已开票"}>{"已开票"}</Option>,
        <Option value={"未开票"}>{"未开票"}</Option>]}</Select>
    </FormField>);
    return arr;
  };

  render() {
    const {sales = {}, _notify, form} = this.props;
    const {showDetail, record = {}} = this.state;

    if (_notify) {
      notify(_notify);
    }
    const {amount, count, array} = sales;
    return (
      <Form className={styles.content}>
        <ViewTitle
          title={`${title}列表`}
          level={1}
          // actions={[<Button onClick={() => this.export()}>导出</Button>,
          //   <Button type="primary" onClick={() => this.onCreateOrEdit(undefined)}>添加</Button>]}
        ></ViewTitle>
        <SearchForm
          style={{marginLeft: '6px', marginRight: '6px'}}
          onSearch={() => {
            this.state.current = 1;
            this.componentDidMount();
          }}
          onReset={this.handleReset}
          cols={3}
          min={6}
          expandText="展开"
          form={form}
        >
          {this.getSearchFields()}
        </SearchForm>
        <div style={{background: '#ECECEC', padding: '30px'}}>
          <Row gutter={16}>
            <Col span={12}>
              <Card>
                <Statistic
                  title="订单商品总数量"
                  value={count}
                  precision={2}
                  valueStyle={{color: '#3f8600'}}
                  suffix="KG"
                />
              </Card>
            </Col>
            <Col span={12}>
              <Card>
                <Statistic
                  title="订单商品总金额"
                  value={amount}
                  precision={2}
                  valueStyle={{color: '#cf1322'}}
                  suffix="元"
                />
              </Card>
            </Col>
          </Row>
        </div>
        <Table
          className={styles.antTable}
          bordered={true}
          columns={this.getTableCols()}
          dataSource={array}
          scroll={{x: true}}
          pagination={{
            current: this.state.current,
            total: this.state.total,
            showSizeChanger: true,
            onChange: page => {
              this.state.current = page;
              this.componentDidMount();
            },
            onShowSizeChange: (current, pageSize) => {
              this.state.current = 1;
              this.state.pageSize = pageSize;
              this.componentDidMount();
            }
          }}
        ></Table>

        {showDetail && <Drawer
          visible={showDetail}
          title={`${title}详情`}
          placement="left"
          width={'80%'}
          style={{padding: 0}}
          onClose={() => {
            this.setState({showDetail: false});
            this.componentDidMount();
          }}
        >
          <ModalSalesOrderDetail
            values={record}
            isReview={true}
          > </ModalSalesOrderDetail>
        </Drawer>}
      </Form>
    );
  }
}
